package trustedPlatformBackup;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

public class ProgramProperties {

	private Properties userProperties = new Properties();;
	private Properties defaultProperties = new Properties(this.defaultProperties);

	public static final String PROPERTIE_FILE = "backupTool.config";
	private String strPropertieFile = PROPERTIE_FILE;

	public static final String DATA_FOLDER_DEFAULT_PROPERTY = "/secure/importantData";
	public static final String DATA_FOLDER_LABEL = "DATA_FOLDER";

	public static final String BACKUP_FOLDER_DEFAULT_PROPERTY = "/secure/mnt/decodedData";
	public static final String BACKUP_FOLDER_LABEL = "BACKUP_FOLDER";

	public static final String CURRENT_BACKUP_FOLDER_DEFAULT_PROPERTY = "currentBackup";
	public static final String CURRENT_BACKUP_FOLDER_LABEL = "CURRENT_BACKUP_FOLDER";

	public static final String INC_BACKUP_FOLDER_DEFAULT_PROPERTY = "backup";
	public static final String INC_BACKUP_FOLDER_LABEL = "INC_BACKUP_FOLDER_LABEL";

	public static final String HARDLINK_MODE_DEFAULT_PROPERTY = "1";
	public static final String HARDLINK_MODE_LABEL = "HARDLINK_MODE";

	public static final String LOG_BACKUP_FOLDER_DEFAULT_PROPERTY = "logfiles";
	public static final String LOG_BACKUP_FOLDER_LABEL = "LOG_BACKUP_FOLDER";

	public static final String ECRYPTFS_MOUNTPOINT_FOLDER_DEFAULT_PROPERTY = "/secure/mnt/decodedData";
	public static final String ECRYPTFS_MOUNTPOINT_FOLDER_LABEL = "ECRYPTFS_MOUNTPOINT_FOLDER";

	public static final String ECRYPTFS_SRC_FOLDER_DEFAULT_PROPERTY = "/secure/mnt/encodedDataLocal";
	public static final String ECRYPTFS_SRC_FOLDER_LABEL = "ECRYPTFS_SRC_FOLDER";

	public static final String TPMSCRIPTS_FOLDER_DEFAULT_PROPERTY = "/secure/trustedbackuptool";
	public static final String TPMSCRIPTS_FOLDER_LABEL = "TPMSCRIPTS_FOLDER";

	public static final String DOBACKUP_SCRIPT_FOLDER_DEFAULT_PROPERTY = "tpm_backup_doBackup.sh";
	public static final String DOBACKUP_SCRIPT_FOLDER_LABEL = "DOBACKUP_SCRIPT_FOLDER";

	public static final String DOBACKUP_RSYNC_PARAMETER_DEFAULT_PROPERTY = "-avzbh --delete ";
	public static final String DOBACKUP_RSYNC_PARAMETER_LABEL = "DOBACKUP_RSYNC_PARAMETER";

	public static final String DOBACKUP_RSYNC_HL_PARAMETER_DEFAULT_PROPERTY = "-avzbh --delete ";
	public static final String DOBACKUP_RSYNC_HL_PARAMETER_LABEL = "DOBACKUP_RSYNC_HL_PARAMETER";
	
	public static final String BACHUP_TOOL_CONFIG_FILE_DEFAULT_PROPERTY = "tpm_backup_config.sh";
	public static final String BACHUP_TOOL_CONFIG_FILE_LABEL = "BACHUP_TOOL_CONFIG_FILE";

	public static final String BACHUP_TOOL_FOLDER_DEFAULT_PROPERTY = "/secure/trustedbackuptool";
	public static final String BACHUP_TOOL_FOLDER_LABEL = "BACHUP_TOOL_FOLDER";
	
	public ProgramProperties() {

		defaultProperties.setProperty(DATA_FOLDER_LABEL, DATA_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(BACKUP_FOLDER_LABEL, BACKUP_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(CURRENT_BACKUP_FOLDER_LABEL, CURRENT_BACKUP_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(INC_BACKUP_FOLDER_LABEL, INC_BACKUP_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(HARDLINK_MODE_LABEL, HARDLINK_MODE_DEFAULT_PROPERTY);
		defaultProperties.setProperty(LOG_BACKUP_FOLDER_LABEL, LOG_BACKUP_FOLDER_DEFAULT_PROPERTY);

		defaultProperties.setProperty(TPMSCRIPTS_FOLDER_LABEL, TPMSCRIPTS_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(ECRYPTFS_SRC_FOLDER_LABEL, ECRYPTFS_SRC_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(ECRYPTFS_MOUNTPOINT_FOLDER_LABEL, ECRYPTFS_MOUNTPOINT_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(HARDLINK_MODE_LABEL, HARDLINK_MODE_DEFAULT_PROPERTY);

		defaultProperties.setProperty(DOBACKUP_SCRIPT_FOLDER_LABEL, DOBACKUP_SCRIPT_FOLDER_DEFAULT_PROPERTY);

		defaultProperties.setProperty(DOBACKUP_RSYNC_PARAMETER_LABEL, DOBACKUP_RSYNC_PARAMETER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(DOBACKUP_RSYNC_HL_PARAMETER_LABEL, DOBACKUP_RSYNC_HL_PARAMETER_DEFAULT_PROPERTY);
		
		defaultProperties.setProperty(BACHUP_TOOL_FOLDER_LABEL, BACHUP_TOOL_FOLDER_DEFAULT_PROPERTY);
		defaultProperties.setProperty(BACHUP_TOOL_CONFIG_FILE_LABEL, BACHUP_TOOL_CONFIG_FILE_DEFAULT_PROPERTY);
		
		
	}

	// HARDLINK_MODE_LABEL
	public static boolean getHARDLINK_MODE_DEFAULT_PROPERTY() {
		return !HARDLINK_MODE_DEFAULT_PROPERTY.equals("0");
	}

	public boolean getHardlinkMode() {
		return !userProperties.getProperty(HARDLINK_MODE_LABEL).equals("0");
	}

	public String getStrHardlinkMode() {
		return userProperties.getProperty(HARDLINK_MODE_LABEL);
	}

	public void setHardlinkMode(boolean b) {
		userProperties.setProperty(HARDLINK_MODE_LABEL, b == true ? "1" : "0");
	}

	// DATA_FOLDER_LABEL
	public String getDataFolder() {
		return userProperties.getProperty(DATA_FOLDER_LABEL);
	}

	public File getDataFolderFile() {
		return new File(userProperties.getProperty(DATA_FOLDER_LABEL));
	}

	public void setDataFolder(String str) {
		userProperties.setProperty(DATA_FOLDER_LABEL, str);
	}

	// BACKUP_FOLDER_LABEL
	public String getBackupFolder() {
		return userProperties.getProperty(BACKUP_FOLDER_LABEL);
	}

	public void setBackupFolder(String str) {
		userProperties.setProperty(BACKUP_FOLDER_LABEL, str);
	}

	// CURRENT_BACKUP_FOLDER_LABEL
	public String getCurrentBackupFolder() {
		return getBackupFolder() + File.separatorChar + userProperties.getProperty(CURRENT_BACKUP_FOLDER_LABEL);
	}

	// INC_BACKUP_FOLDER_LABEL
	public String getIncBackupFolder() {
		return getBackupFolder() + File.separatorChar + userProperties.getProperty(INC_BACKUP_FOLDER_LABEL);
	}

	// LOG_BACKUP_FOLDER_LABEL
	public String getLogBackupFolder() {
		return getBackupFolder() + File.separatorChar + userProperties.getProperty(LOG_BACKUP_FOLDER_LABEL);
	}

	// public void setLogBackupFolder(String str) {
	// userProperties.setProperty(LOG_BACKUP_FOLDER_LABEL,str);
	// }

	// ECRYPTFS_SRC_FOLDER_LABEL
	public String getEcryptFSSRCFolder() {
		return userProperties.getProperty(ECRYPTFS_SRC_FOLDER_LABEL);
	}

	public File getEcryptFSSRCFolderFile() {
		return new File(userProperties.getProperty(ECRYPTFS_SRC_FOLDER_LABEL));
	}

	public void setEcryptFSSRCFolder(String str) {
		userProperties.setProperty(ECRYPTFS_SRC_FOLDER_LABEL, str);
	}

	// ECRYPTFS_MOUNTPOINT_FOLDER_LABEL
	public String getEcryptFSMountPointFolder() {
		return userProperties.getProperty(ECRYPTFS_MOUNTPOINT_FOLDER_LABEL);
	}

	public File getEcryptFSMountPointFolderFile() {
		return new File(userProperties.getProperty(ECRYPTFS_MOUNTPOINT_FOLDER_LABEL));
	}

	public void setEcryptFSMountPointFolder(String str) {
		userProperties.setProperty(ECRYPTFS_MOUNTPOINT_FOLDER_LABEL, str);
	}

	// TPMSCRIPTS_FOLDER_LABEL
	public String getTPMScriptsFolder() {
		return userProperties.getProperty(TPMSCRIPTS_FOLDER_LABEL);
	}

	public File getTPMScriptsFolderFile() {
		return new File(userProperties.getProperty(TPMSCRIPTS_FOLDER_LABEL));
	}

	public void setTPMScriptsFolderFolder(String str) {
		userProperties.setProperty(TPMSCRIPTS_FOLDER_LABEL, str);
	}
	public String getBackupConfigFile() {
		return userProperties.getProperty(BACHUP_TOOL_FOLDER_LABEL)+File.separator+userProperties.getProperty(BACHUP_TOOL_CONFIG_FILE_LABEL);
	}

	public void load(String strPropertieFileName) {

		if (strPropertieFileName != null) {
			strPropertieFile = strPropertieFileName;
		}

		File propertieFile = new File(strPropertieFile);

		try {
			if (!propertieFile.isFile()) {
				FileOutputStream propOutFile = new FileOutputStream(propertieFile);
				defaultProperties.store(propOutFile, "Eigenschaften für TrustedBackup Tool");
				propOutFile.close();
			}
			FileInputStream propInFile = new FileInputStream(strPropertieFile);
			userProperties.load(propInFile);
			propInFile.close();
		} catch (IOException e) {

			e.printStackTrace();
		}

	}

	public void save(String strPropertieFileName) {
		if (strPropertieFileName != null) {
			strPropertieFile = strPropertieFileName;
		}

		File propertieFile = new File(strPropertieFile);
		try {
			FileOutputStream propOutFile = new FileOutputStream(propertieFile);
			userProperties.store(propOutFile, "Eigenschaften für TrustedBackup Tool");
			propOutFile.close();
			
			saveSHConfig();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public File getIncBackupFolderFile() {
		return (new File(getIncBackupFolder()));

	}

	public String getDoBackupScript() {
		return getTPMScriptsFolder() + File.separator + userProperties.getProperty(DOBACKUP_SCRIPT_FOLDER_LABEL);

	}

	public void saveSHConfig() {

	

		ArrayList<String> configArray = new ArrayList<String>();
		configArray.add("# tpm_backup_config.sh ");

		configArray.add("DATA_FOLDER=\"" + getDataFolder() + "\"");
		configArray.add("BACKUP_FOLDER=\"" + getBackupFolder() + "\"");
		configArray.add("hardlinkMode=" + getStrHardlinkMode());

		configArray.add("incBackupFolder=\"" + getIncBackupFolder() + "\"");
		configArray.add("logfilesBackupFolder=\"" + getLogBackupFolder() + "\"");
		configArray.add("currentBackupFolder=\"" + getCurrentBackupFolder() + "\"");
		configArray.add("fullBackupFolder=\"" + getIncBackupFolder() + File.separator +"${backup_date}_full" + "\"");
		configArray.add("diffBackupFolder=\"${incBackupFolder}/${backup_date}_diff\"");
		configArray.add("diffBackupFolderDir=\"${incBackupFolder}/${backup_date}_diffSF\"");
		configArray.add("doBackupBackupFolder=\"\"");
		configArray.add("doBackupBackupFile=\"\"");

		configArray.add("diffHardlinkBackupFolder=\"${incBackupFolder}/${backup_date}_diff\"");

		configArray.add("rsyncParamenter=\"" + userProperties.getProperty(DOBACKUP_RSYNC_PARAMETER_LABEL) + "\"");
		configArray.add("rsyncParamenterDiffBackupFolder=\"--backup-dir=${diffBackupFolder}\"");
		configArray.add("rsyncParamenterDiffBackupFolderDir=\"--backup-dir=\"");
		

		configArray.add("# hard link mod");
		configArray.add("rsyncParamenterHardlinkBackupDir=\"--link-dest=\"");
		configArray.add("rsyncParamenterHardlinkModeBackupFolder=\"--link-dest=$currentBackupFolder\"");
		configArray.add("rsyncParamenterHardlinkMode=\""+  userProperties.getProperty(DOBACKUP_RSYNC_HL_PARAMETER_LABEL) +"\"");
		configArray.add("diffBackupFolderHardlinkMode=\"${incBackupFolder}/${backup_date}_hard\"");
		
		try {
			BufferedWriter b = new BufferedWriter(new FileWriter(getBackupConfigFile()));

			for (String string : configArray) {
				b.write(string);
				b.newLine();
			}
			

			b.close();
		} catch (IOException e) {
			System.out.println("Fehler: " + e.toString());
		}
	}
}
